﻿
namespace Patterns4Net.Tests.PatternEnforcer.Core
{
    using System.Linq;
    using NUnit.Framework;
    using Patterns4Net.PatternEnforcer.Core;

    [TestFixture]
    public class CheckerResultTest
    {
        [Test]
        public void ErrorsCount_And_CheckedRulesCount_Are_The_Same_As_Given_To_Constructor_When_There_Are_No_Children()
        {
            var result = new CheckerResult(10, new[] { new CheckerError("message") });
            Assert.AreEqual(10, result.CheckedRulesCount);
            Assert.AreEqual(1, result.ErrorsCount);
        }

        [Test]
        public void ErrorsCount_Composition()
        {
            var inner = new CheckerResult(1, new[] { new CheckerError("inner", 2) });
            var result = new CheckerResult(10, new[] { new CheckerError("main") }, new[] { inner });
            Assert.AreEqual(3, result.ErrorsCount);
            CollectionAssert.AreEquivalent(new[] { "inner", "main" }, result.Errors.Select(x => x.Message));
        }

        [Test]
        public void CheckedRulesCount_Composition()
        {
            var inner = new CheckerResult(3, new CheckerError[0]);
            var result = new CheckerResult(5, new[] { new CheckerError("main") }, new[] { inner });
            Assert.AreEqual(8, result.CheckedRulesCount);
        }

        [Test]
        public void IsValid_When_ErrorsCount_Is_Zero_And_No_Children()
        {
            var result = new CheckerResult(3, CheckerError.EmptyArray);
            Assert.IsTrue(result.IsValid);
        }
        
        [Test]
        public void IsValid_When_ErrorsCount_Is_Zero_And_With_One_Child()
        {
            var result = new CheckerResult(new[] { new CheckerResult(0, CheckerError.EmptyArray) });
            Assert.IsTrue(result.IsValid);
        }

        [Test]
        public void IsNotValid_When_ErrorsCount_Is_NonZero()
        {
            var result = new CheckerResult(3, new[] { new CheckerError("error") });
            Assert.IsFalse(result.IsValid);
        }

        [Test]
        public void SimpleResult_FactoryMethod_With_True_Produces_One_CheckedRule_And_Zero_Errors()
        {
            var result = CheckerResult.CreateSimpleResult(true, new CheckerError("error"));
            Assert.AreEqual(1, result.CheckedRulesCount);
            Assert.AreEqual(0, result.ErrorsCount);
        }

        [Test]
        public void SimpleResult_FactoryMethod_With_False_Produces_One_CheckedRule_And_One_Errors()
        {
            var result = CheckerResult.CreateSimpleResult(false, new CheckerError("error"));
            Assert.AreEqual(1, result.CheckedRulesCount);
            Assert.AreEqual(1, result.ErrorsCount);            
        }
    }
}
